package com.wfh.activity;

import java.util.ArrayList;

import com.wfh.R;
import com.wfh.adapter.ShopsAdapter;
import com.wfh.bean.ShopBean;
import com.wfh.service.CoreService;
import com.wfh.service.CoreService.MyBinder;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.os.IBinder;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ShopsActivity extends CommonActivity {

	private MyDialog dialog;
	private ArrayList<ShopBean> list;
	private ShopsAdapter adapter;
	private ListView listview;
	private boolean mbundle = true;
	private BroadcastReceiver datareceiver;
	private TextView current;
	private CoreService service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_shops);

		listview = (ListView) findViewById(R.id.listView1);
		current = (TextView) findViewById(R.id.current);

		current.setText("您当前选择的店铺是:" + Util.getShare(this, "shop_name"));

		dialog = MyDialog.create(this);

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_SHOPS);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);

		// 绑定service
		Util.bindservice(this, CoreService.class, conn);

		dialog.show();

		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Util.saveShare(ShopsActivity.this, "shop_id", list
						.get(position).getShop_id());
				Util.saveShare(ShopsActivity.this, "shop_name",
						list.get(position).getShop_name());
				Log.i("xxx", list.get(position).getLongitude());
				Util.saveShare(ShopsActivity.this, "longitude",
						list.get(position).getLongitude());
				Util.saveShare(ShopsActivity.this, "latitude",
						list.get(position).getLatitude());
				
				Util.startactivity(ShopsActivity.this, MainActivity.class);
				ShopsActivity.this.finish();
			}
		});

	}

	// 监听返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Util.startactivity(ShopsActivity.this, MainActivity.class);
			ShopsActivity.this.finish();
			return true;
		}
		return false;
	}

	// 绑定服务service
	private ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			service = ((MyBinder) binder).getService();
			service.getLocalShops(ShopsActivity.this, null);
		}
	};

	// 接收器
	private class DataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			list = bd.getParcelableArrayList("shops");
			if (list == null || list.size() == 0)
				return;
			if (adapter != null) {
				adapter.list = list;
				adapter.notifyDataSetChanged();
//				Util.setListViewHeightBasedOnChildren(listview);
				dialog.hide();
				mbundle = true;
				return;
			}
			adapter = new ShopsAdapter(ShopsActivity.this, list);
			listview.setAdapter(adapter);
//			Util.setListViewHeightBasedOnChildren(listview);
			dialog.hide();
			mbundle = true;
		}
	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if (conn != null)
			this.getApplicationContext().unbindService(conn);
		unregisterReceiver(datareceiver);
		super.onDestroy();
	}

}
